/*
 * Things to highlight in the example:
 * 1. applying Option to avoid null case, which usually crash system
 * 2. getOrElse() make codes nicer
 * 3. even if scala still support "null"(for the sake of compatibility with java & c#)
 *    don't ever add "null" into new code, which is proved "billion dollar mistake"
 */

package helloscala.practise.chapter2

object OptionSomeNone {
  def avoidNull: Unit = {
    val personTitle = Map("Jeff" -> "Dad",
                          "Winnie" -> "Mom",
                          "Cameron" -> "son")
    
    println("Get Option vals")
    println("Get Jeff: " + personTitle.get("Jeff"))
    println("Get Mike: " + personTitle.get("Mike"))

    println("Get Out of options")
    println("Get Winnie: " + personTitle.get("Winnie").get)
    println("Get Cameron: " + personTitle.get("Cameron").getOrElse("Opps1 !"))
    println("Get Mike: " + personTitle.get("Mike").getOrElse("Opps2 !"))
  }

  /**
   * src of scala.collection.immutable.HashMap.get()
   */
//  def get(key: A): Option[B] = {
//    if (contains(key))
//      new Some(getValue(key))
//    else
//      None
//  }
}
